package com.ztgf.sms.api.cms.sms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sms.models.bo.SmsUserBO;
import com.ztgf.sms.models.bo.SmsUserSaveBO;
import com.ztgf.sms.models.dto.sms.SmsInfoDTO;
import com.ztgf.sms.models.dto.sms.SmsUserDTO;
import com.ztgf.sms.models.service.ISmsInfoService;
import com.ztgf.sms.models.service.ISmsUserService;
import com.ztgf.sms.models.vo.SmsUserNewVO;
import com.ztgf.sms.models.vo.SmsUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 文件用户表控制器
 *
 * @author mxli
 * @Date 2020-10-15 14:32:14
 */

@RestController
@RequestMapping("/cms/SmsUser")
@Slf4j
@Api(value = "SmsUserController", tags = "文件用户表接口")
public class SmsUserController {

    @DubboReference
    private ISmsUserService SmsUserService;

    @DubboReference
    private ISmsInfoService smsInfoService;

    @ApiOperation(value = "添加文件用户表", notes = "添加文件用户表")
    @PostMapping(value = "/insert")
    public Result<?> insert(SmsUserSaveBO saveBO) {
        if (StrUtils.isEmpty(saveBO.getAccount()) || StrUtils.isEmpty(saveBO.getPassword()) || saveBO.getPassword().length() != 32) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<SmsUserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account", saveBO.getAccount());
        SmsUserDTO dto = SmsUserService.selectOne(wrapper);
        //验证数据库是否有相同的用户名
        if (dto != null) {
            return new Result<>(CodeEnum.FAIL_USER_200002);
        }
        boolean flag = SmsUserService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存文件用户表", notes = "保存文件用户表")
    public Result<?> save(SmsUserSaveBO saveBO) {
        if (saveBO.getId() == null) {
            //添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @ApiOperation(value = "修改文件用户表", notes = "修改文件用户表")
    @PostMapping("/update")
    public Result<?> update(SmsUserSaveBO saveBO) {
        SmsUserDTO dto = new SmsUserDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setPassword(null);
        dto.setSalt(null);
        dto.setId(saveBO.getId());
        boolean flag = SmsUserService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "修改文件用户表", notes = "修改文件用户表")
    @PostMapping("/updateFileSzie")
    public Result<?> update(Long userId, Long fileSize) {
        SmsUserDTO dto = new SmsUserDTO();
        dto.setFileSize(fileSize);
        dto.setId(userId);
        boolean flag = SmsUserService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/updatePassword")
    public Result<?> update(String oldPassword, String newPassword, @RequestParam(value = "name", required = false) String name) {
        if (StrUtils.isEmpty(oldPassword) || StrUtils.isEmpty(newPassword)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return SmsUserService.updateUserPassword(oldPassword, newPassword, name);
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
            , @ApiImplicitParam(name = "status", value = "状态 0启用，1冻结、2删除", required = true, paramType = "query", dataType = "Integer")
    })
    public Result<?> updateStatus(Long id, Integer status) {
        SmsUserDTO dto = new SmsUserDTO();
        dto.setId(id);
        dto.setStatus(status);
        boolean flag = SmsUserService.updateById(dto);
        if (status == 1) {//冻结状态 删除session
            SmsUserService.delUserSessionById(id);
        }
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除文件用户表", notes = "删除文件用户表")
    @ApiImplicitParam(name = "id", value = "文件用户表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam("id") Long id) {
        boolean flag = SmsUserService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除文件用户表", notes = "批量删除文件用户表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        SmsUserService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<SmsUserDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SmsUserDTO dto = SmsUserService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "文件用户表列表", notes = "文件用户表列表")
    @PostMapping("/queryPageList")
    public Result<?> queryPageList(SmsUserBO bo) {
        Wrapper<SmsUserDTO> wrapper = new EntityWrapper<>();
        if (StrUtils.isNotEmpty(bo.getAccount())) {
            wrapper.like("account", bo.getAccount());
        }
        if (StrUtils.isNotEmpty(bo.getName())) {
            wrapper.like("name", bo.getName());
        }
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());
        }
        Page<SmsUserDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = SmsUserService.selectPage(page, wrapper);
        Page<SmsUserNewVO> newVOPage = new Page<>(bo.getPageNo(), bo.getPageSize());
        newVOPage.setSize(page.getSize());
        newVOPage.setTotal(page.getTotal());
        newVOPage.setCurrent(page.getCurrent());
        List<SmsUserNewVO> list = new ArrayList<>();
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            for (SmsUserDTO dto : page.getRecords()) {
                SmsUserNewVO smsUserNewVO = new SmsUserNewVO();
                BeanUtils.copyProperties(dto, smsUserNewVO);
                smsUserNewVO.setPassword(null);
                smsUserNewVO.setSalt(null);
                // 获取发送条数
                Wrapper<SmsInfoDTO> smsInfoDTOWrapper = new EntityWrapper<>();
                smsInfoDTOWrapper.eq("company_id", dto.getId());
                int count = smsInfoService.selectCount(smsInfoDTOWrapper);
                smsUserNewVO.setPushNumber(count);
                list.add(smsUserNewVO);
            }
            newVOPage.setRecords(list);
        }
        return new Result<>(newVOPage);
    }


    @ApiOperation(value = "用户文件使用大小统计", notes = "用户文件使用大小统计")
    @PostMapping("/queryPageVOList")
    public Result<Page<SmsUserVO>> queryPageVOList(SmsUserBO bo) {
        Wrapper<SmsUserVO> wrapper = new EntityWrapper<>();
        if (StrUtils.isNotEmpty(bo.getAccount())) {
            wrapper.like("fu.account", bo.getAccount());
        }
        if (StrUtils.isNotEmpty(bo.getName())) {
            wrapper.like("fu.name", bo.getName());
        }
        if (bo.getStatus() != null) {
            wrapper.eq("fu.status", bo.getStatus());
        }
        if (bo.getStartDate() != null) {
            wrapper.ge("fu.create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("fu.create_time", bo.getEndDate());
        }
        Page<SmsUserVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = SmsUserService.queryPageVOList(page, wrapper, bo.getType());
        return new Result<>(page);
    }
}
